package 文件io;
import java.io.*;
/*
 java.io 运用装饰者设计模式给流添加功能和便利性
 例如
 OutputStream 基类->>FileOutputStream 具体被装饰的类->>FilterOutputStream 装饰者基类->
 >BufferedOutputStream/PrintStream 装饰者类
 用法  new PrintStream(new BufferedOutputStream(new FileOutputStream("xxx")))
 给字节输出流一次套上了缓冲流和打印流，增强了缓存性能和打印的遍历性
 */
public class 装饰者设计模式
{
	public static void main(String[] args){
		Drink coffee=new Coffee();//原味咖啡
		System.out.println("咖啡类型: "+coffee.info()+" 价格: "+coffee.price());
		//通过装饰者类添加各种口味
		Milk milk=new Milk(coffee);//加奶咖啡
		System.out.println("咖啡类型: "+milk.info()+" 价格: "+milk.price());
		Sugar sugar=new Sugar(milk);
		System.out.println("咖啡类型: "+sugar.info()+" 价格: "+sugar.price());
		Ice ice=new Ice(sugar);
		System.out.println("咖啡类型: "+ice.info()+" 价格: "+ice.price());
	}
}
//被装饰的基类/接口
interface Drink
{
	float price();
	String info();
}
//具体被装饰的类
class Coffee implements Drink
{

	@Override
	public float price()
	{

		return 10f;
	}

	@Override
	public String info()
	{

		return "原味";
	}


}
//装饰者基类

abstract class Decorator implements Drink
{
	protected Drink drink;

	public Decorator(Drink drink)
	{
		this.drink = drink;
	}
	@Override
	public float price()
	{
		return drink.price();
	}

	@Override
	public String info()
	{
		return drink.info();
	}
}
//装饰者类
class Milk extends Decorator
{
	public Milk(Drink drink)
	{
		super(drink);
	}

	@Override
	public float price()
	{
		// TODO: Implement this method
		return super.price() + 2.5f;
	}

	@Override
	public String info()
	{
		// TODO: Implement this method
		return super.info() + "+奶";
	}

}
class Sugar extends Decorator
{
	public Sugar(Drink drink)
	{
		super(drink);
	}

	@Override
	public float price()
	{
		// TODO: Implement this method
		return super.price() + 1.5f;
	}

	@Override
	public String info()
	{
		// TODO: Implement this method
		return super.info() + "+糖";
	}

}
class Ice extends Decorator
{
	public Ice(Drink drink)
	{
		super(drink);
	}

	@Override
	public float price()
	{
		// TODO: Implement this method
		return super.price() + 1f;
	}

	@Override
	public String info()
	{
		// TODO: Implement this method
		return super.info() + "+冰";
	}

}


